package myRedis

import (
	"chart.zozoo.net/core/conf"
	"github.com/go-redis/redis"
	"time"
)

var RedisClient *redis.Client

func ConnRedis(c *conf.Redis) *redis.Client {
	if c.Addr == "" {
		panic("redis地址不能为空")
	}

	client := redis.NewClient(&redis.Options{
		Addr:         c.Addr,
		PoolSize:     c.PoolSize,
		Password:     c.Password,
		IdleTimeout:  time.Duration(c.IdleTimeout) * time.Millisecond,
		ReadTimeout:  time.Duration(c.ReadTimeout) * time.Millisecond,
		WriteTimeout: time.Duration(c.WriteTimeout) * time.Millisecond,
		MaxRetries:   c.Retries,
		MinIdleConns: c.MinIdleConns,
		DB:           c.Db,
	})

	_, err := client.Ping().Result()
	if err != nil {
		panic("redis连接失败：" + err.Error())
	}
	RedisClient = client
	return client
}
